package com.ziven.common.widget.app;

import androidx.activity.ComponentActivity;
import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;

import com.ziven.easygo.design.funnel.EasyFilter;
import com.ziven.easygo.design.funnel.FilterHelper;
import com.ziven.easygo.design.funnel.IFilter;
import com.ziven.easygo.design.funnel.OneFilterGrain;
import com.ziven.easygo.util.LazyInit;
import com.ziven.easygo.util.LogHelper;

/**
 * @author Administrator
 */
public final class EasySpate implements LifecycleEventObserver {

    private static final String CREATE = "EasySpate_CREATE";
    private static final String START = "EasySpate_START";
    private static final String RESUME = "EasySpate_RESUME";
    private static final String PAUSE = "EasySpate_PAUSE";
    private static final String STOP = "EasySpate_STOP";
    private static final String DESTROY = "EasySpate_DESTROY";

    public static final LazyInit<EasySpate> SPATE = LazyInit.lazy(EasySpate::new);

    private EasySpate() {}

    private EasySpate(@NonNull ComponentActivity activity) {
        activity.getLifecycle().addObserver(this);
    }

    public static EasySpate newInstance() {
        return new EasySpate();
    }

    public static EasySpate newInstance(@NonNull ComponentActivity activity) {
        return new EasySpate(activity);
    }

    public static EasySpate spate() {
        return SPATE.value();
    }

    public static EasySpate addObserver(@NonNull ComponentActivity activity) {
        activity.getLifecycle().addObserver(SPATE.value());
        return spate();
    }

    public static EasySpate removeObserver(@NonNull ComponentActivity activity) {
        SPATE.value().clearFilter(activity);
        return spate();
    }

    public static void addSpate(@NonNull EasyFilter filter,
                                @NonNull Spate spate) {
        filter.add(spate);
    }

    public EasySpate addCreateSpate(@NonNull Spate spate) {
        addSpate(createFilter(), spate);
        return this;
    }

    public EasySpate addStartSpate(@NonNull Spate spate) {
        addSpate(startFilter(), spate);
        return this;
    }

    public EasySpate addResumeSpate(@NonNull Spate spate) {
        addSpate(resumeFilter(), spate);
        return this;
    }

    public EasySpate addPauseSpate(@NonNull Spate spate) {
        addSpate(pauseFilter(), spate);
        return this;
    }

    public EasySpate addStopSpate(@NonNull Spate spate) {
        addSpate(stopFilter(), spate);
        return this;
    }

    public EasySpate addDestroySpate(@NonNull Spate spate) {
        addSpate(destroyFilter(), spate);
        return this;
    }

    public EasyFilter createFilter() {
        return FilterHelper.get().getIfNullObtainEmpty(activityId(CREATE));
    }

    public EasyFilter startFilter() {
        return FilterHelper.get().getIfNullObtainEmpty(activityId(START));
    }

    public EasyFilter resumeFilter() {
        return FilterHelper.get().getIfNullObtainEmpty(activityId(RESUME));
    }

    public EasyFilter pauseFilter() {
        return FilterHelper.get().getIfNullObtainEmpty(activityId(PAUSE));
    }

    public EasyFilter stopFilter() {
        return FilterHelper.get().getIfNullObtainEmpty(activityId(STOP));
    }

    public EasyFilter destroyFilter() {
        return FilterHelper.get().getIfNullObtainEmpty(activityId(DESTROY));
    }

    private String activityId(@NonNull String event) {
        String id = event + "_" + hashCode();
        LogHelper.log("EasySpate: id=" + id);
        return id;
    }

    public EasySpate clearCreateFilter() {
        FilterHelper.get().clearFilter(activityId(CREATE));
        return this;
    }

    public EasySpate clearStartFilter() {
        FilterHelper.get().clearFilter(activityId(START));
        return this;
    }

    public EasySpate clearResumeFilter() {
        FilterHelper.get().clearFilter(activityId(RESUME));
        return this;
    }

    public EasySpate clearPauseFilter() {
        FilterHelper.get().clearFilter(activityId(PAUSE));
        return this;
    }

    public EasySpate clearStopFilter() {
        FilterHelper.get().clearFilter(activityId(STOP));
        return this;
    }

    public EasySpate clearDestroyFilter() {
        FilterHelper.get().clearFilter(activityId(DESTROY));
        return this;
    }

    public EasySpate clearFilter() {
        clearCreateFilter();
        clearStartFilter();
        clearResumeFilter();
        clearPauseFilter();
        clearStopFilter();
        clearDestroyFilter();
        return this;
    }

    public EasySpate clearFilter(@NonNull ComponentActivity activity) {
        activity.getLifecycle().removeObserver(this);
        clearFilter();
        return this;
    }

    public interface Spate extends IFilter<OneFilterGrain> {

        /**
         * Filter
         * @param grain OneFilterGrain
         */
        @Override
        default void filter(@NonNull OneFilterGrain grain) {
            LogHelper.log("EasySpate: filter...");
            spate();
        }

        /**
         * Spate
         */
        void spate();
    }

    @Override
    public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
        LogHelper.log("EasySpate: event=" + event);
        switch (event) {
            case ON_CREATE:
                createFilter().filter(OneFilterGrain.of());
                break;
            case ON_START:
                startFilter().filter(OneFilterGrain.of());
                break;
            case ON_RESUME:
                resumeFilter().filter(OneFilterGrain.of());
                break;
            case ON_PAUSE:
                pauseFilter().filter(OneFilterGrain.of());
                break;
            case ON_STOP:
                stopFilter().filter(OneFilterGrain.of());
                break;
            case ON_DESTROY:
                destroyFilter().filter(OneFilterGrain.of());
                break;
            default:
                break;
        }
    }
}
